//
//  UIImage+Category.m
//  Teleport
//
//  Created by xtjun on 2020/5/25.
//  Copyright © 2020 xxt. All rights reserved.
//

#import "UIImage+Category.h"

@implementation UIImage (Category)

//
//- (void)asynImageSize:(CGSize)size radius:(CGFloat)radius backColor:(UIColor *)backColor completion:(void(^)(UIImage *image))completion{

//
//}

- (UIImage *)imageSize:(CGSize)size color:(UIColor *)color path:(UIBezierPath *)path {
    // 利用绘图建立上下文
    UIGraphicsBeginImageContextWithOptions(size, true, 0);
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    // 填充颜色
    [color setFill];
    UIRectFill(rect);
    // 贝塞尔裁切
    [path addClip];
    [self drawInRect:rect];
    
    // 获取结果
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    // 关闭上下文
    UIGraphicsEndImageContext();
    // 主队列回调
    return resultImage;
}

- (void)asynImageSize:(CGSize)size color:(UIColor *)color
         finishiBlock:(void (^)(UIImage *))finishiBlock {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
        [color setFill];
        CGRect rect = CGRectMake(0, 0, size.width, size.height);
        UIRectFill(rect);
        [self drawInRect:rect];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        dispatch_async(dispatch_get_main_queue(), ^{
            if (finishiBlock) finishiBlock(newImage);
        });
    });
}


- (UIImage *)imageSize:(CGSize)size color:(UIColor *)color {
    return [self imageSize:size color:color scale:[UIScreen mainScreen].scale];
}

- (UIImage *)imageSize:(CGSize)size color:(UIColor *)color scale:(CGFloat)scale {
    UIGraphicsBeginImageContextWithOptions(size, NO, scale);
    [color setFill];
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIRectFill(rect);
    [self drawInRect:rect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

- (UIImage *)changeColor:(UIColor *)color {
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
    [color setFill];
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
    UIRectFill(bounds);
    [self drawInRect:bounds blendMode:kCGBlendModeOverlay alpha:1.0f];
    [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

+ (UIImage *)imageWithColor:(UIColor *)color {
    return [self imageWithColor:color size:CGSizeMake(1, 1)];
}

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

#pragma mark - 生产二维码

// 根据一个字符串生成一个二维码
+ (UIImage *)qrCodeImageWithContent:(NSString *)content withSize:(CGFloat)size {
    NSData *codeData = [content dataUsingEncoding:NSUTF8StringEncoding];
    //  使用CIQRCodeGenerator创建filter
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator" withInputParameters:@{@"inputMessage": codeData, @"inputCorrectionLevel": @"H"}];
    // 由filter.outputImage直接转成的UIImage不太清楚，需要做高清处理
    UIImage *codeImage = [self createNonInterpolatedUIImageFormCIImage:filter.outputImage withSize:CGSizeMake(size, size)];
    return codeImage;
}

// 生成清晰的二维码
+ (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGSize) size {
    
    //! 将CIImage转成CGImageRef
    CGRect integralRect = image.extent;// CGRectIntegral(image.extent);// 将rect取整后返回，origin取舍，size取入
    CGImageRef imageRef = [[CIContext context] createCGImage:image fromRect:integralRect];
    
    //! 创建上下文
    CGFloat sideScale = fminf(size.width / integralRect.size.width, size.width / integralRect.size.height) * [UIScreen mainScreen].scale;// 计算需要缩放的比例
    size_t contextRefWidth = ceilf(integralRect.size.width * sideScale);
    size_t contextRefHeight = ceilf(integralRect.size.height * sideScale);
    CGContextRef contextRef = CGBitmapContextCreate(nil, contextRefWidth, contextRefHeight, 8, 0, CGColorSpaceCreateDeviceGray(), (CGBitmapInfo)kCGImageAlphaNone);// 灰度、不透明
    CGContextSetInterpolationQuality(contextRef, kCGInterpolationNone);// 设置上下文无插值
    CGContextScaleCTM(contextRef, sideScale, sideScale);// 设置上下文缩放
    CGContextDrawImage(contextRef, integralRect, imageRef);// 在上下文中的integralRect中绘制imageRef
    
    //! 从上下文中获取CGImageRef
    CGImageRef scaledImageRef = CGBitmapContextCreateImage(contextRef);
    CGContextRelease(contextRef);
    CGImageRelease(imageRef);
    
    //! 将CGImageRefc转成UIImage
    return [UIImage imageWithCGImage:scaledImageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
    
}

- (UIImage *)resizable {
    return  [self resizableImageWithCapInsets:UIEdgeInsetsMake(self.size.height * 0.5 - 1, self.size.width * 0.5 - 1, self.size.height * 0.5 + 1, self.size.width * 0.5 + 1) resizingMode:UIImageResizingModeStretch];
}
 
@end
